import Router from 'koa-router';
import proxyService from '../../../common/proxyService';
import httpsXmService from '../../../common/httpsXmService';
// const config = require('config-lite')({
//   filename: 'default',
//   config_basedir: __dirname,
//   config_dir: 'config',
// });
// const { wx } = config;

import {
  getWxPayParams, //
  getWxPaySign, //
  wxCreateOrder, //
  offlineCreateOrder,
} from '../../../api';

const router = new Router();

// 获取支付参数
router.post('/getWxPayParams', async ctx => {
  // delete ctx.requestParams.carry
  // console.log('获取支付参数1', ctx.requestParams);
  const response = await proxyService.postProxy(ctx, getWxPayParams);
  const responseData = {
    code: response.code,
    data: {
      xml: response.data,
      // key: wx.paySecret,
    },
  };
  return (ctx.body = responseData);
});

// 微信统一支付接口
router.post('/getWxPay', async ctx => {
  // console.log('统一支付', ctx.requestParams.xml);
  const response = await httpsXmService.post(ctx.requestParams.xml);
  return (ctx.body = response);
});

// 获取支付签名
router.post('/getWxPaySign', async ctx => {
  const response = await proxyService.postProxy(ctx, getWxPaySign);
  return (ctx.body = response);
});

// 生成微信支付订单
router.post('/wxCreateOrder', async ctx => {
  const response = await proxyService.postProxy(ctx, wxCreateOrder);
  return (ctx.body = response);
});

// 生成线下支付订单
router.post('/offlineCreateOrder', async ctx => {
  const response = await proxyService.postProxy(ctx, offlineCreateOrder);
  return (ctx.body = response);
});

export default router;
